package com.sctu.EMSApp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.MyinfoManageOneNote;
import com.sctu.bean.TravelNotesListPostBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;
import com.sctu.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zhengsenwen on 2016/1/5.
 */
public class MyinfoManageOneNoteActivity extends BaseActivity {

    private TextView headtitleitext;
    private ImageView returnfragment;
    private TextView datetext;
    private TextView authortext;
    private TextView readtext;
    private TextView commenttext;
    private TextView praisetext;

    private TextView conetent;

    private ImageView forwardimg;
    private ImageView commentimg;
    private ImageView praiseimg;

    private ImageView sendCommentImg;
    private TextView editText;

    private LinearLayout commentLinearLayout;

    private ImageView imageView;
    private List<View> listviews;
    private List<ImageView> listimageView = new ArrayList<ImageView>();
    private ViewPager viewPager;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myinfo_manage_notes_details);
        initViews();
//        sendComment();
    }

    private void initViews() {
        int id = getIntent().getIntExtra("id", 0);
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL_SCENIC_SURROUND_ONE + id, listener, errorlistener);
    }

    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("notejson", json);

            MyinfoManageOneNote myinfoManageOneNote = JSON.parseObject(json, MyinfoManageOneNote.class);
            final List<MyinfoManageOneNote.Results> results = myinfoManageOneNote.getResults();

            view = LayoutInflater.from(MyinfoManageOneNoteActivity.this).inflate(R.layout.activity_myinfo_manage_notes_details, null);
            headtitleitext = (TextView) findViewById(R.id.myinfo_manage_notes_details_title_txt);
            headtitleitext.setText(results.get(0).getTitle());

            returnfragment = (ImageView) findViewById(R.id.myinfo_manage_notes_details_return_img);
            returnfragment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyinfoManageOneNoteActivity.this.finish();
                }
            });
            datetext = (TextView) findViewById(R.id.myinfo_manage_notes_details_date_txt);
            datetext.setText(results.get(0).getPubdate());

            authortext = (TextView) findViewById(R.id.myinfo_manage_notes_details_author_name);
            authortext.setText(results.get(0).getAuthor());

            //阅读数 评论数 赞数
//            readtext = (TextView) findViewById(R.id.activity_myinfo_details_read_txt);
//            readtext.setText("评论："+  results.get(0).get);

//            commenttext = (TextView) findViewById(R.id.activity_myinfo_details_comment_txt);
//            commenttext.setText("评论："+ Integer.toString(results.get(0).getCommentJsonList().size()));

//            praisetext = (TextView) findViewById(R.id.activity_myinfo_details_praise_txt);
//            praisetext.setText("评论："+  results.get(0).getAuthor());

            conetent = (TextView) findViewById(R.id.myinfo_manage_notes_details_content);
            conetent.setText(results.get(0).getContent());

            //阅读 评论 赞
            forwardimg = (ImageView) findViewById(R.id.myinfo_manage_notes_details_forward_img);
            forwardimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MyinfoManageOneNoteActivity.this, "转载", Toast.LENGTH_LONG)
                            .show();
                }
            });

            commentimg = (ImageView) findViewById(R.id.myinfo_manage_notes_details_comment_img);
            commentimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MyinfoManageOneNoteActivity.this, "评论", Toast.LENGTH_LONG)
                            .show();
                }
            });

            praiseimg = (ImageView) findViewById(R.id.myinfo_manage_notes_details_praise_img);
            praiseimg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MyinfoManageOneNoteActivity.this, "赞", Toast.LENGTH_LONG)
                            .show();
                }
            });

            listviews = new ArrayList<View>();
            for (int i = 0; i < results.get(0).getFilesList().size(); i++) {
                View itemviewpagerview = LayoutInflater.from(MyinfoManageOneNoteActivity.this).inflate(R.layout.item_myinfo_manage_note_details_viewpager, null);
                imageView = (ImageView) itemviewpagerview.findViewById(R.id.item_myinfo_manage_note_details_image);
                //获取网络图片并加载到相应的view上面
                GetVolleyHttp.getInstance(MyinfoManageOneNoteActivity.this).
                        setImageLoader(imageView, R.drawable.error_image,
                                R.drawable.error_image, UrlConfig.URL + results.get(0).getFilesList().get(i).getPath());
                //添加到image集合中
                listviews.add(itemviewpagerview);

            }
            //获取viewpager
            viewPager = (ViewPager) findViewById(R.id.myinfo_manage_notes_details_viewpager);
            viewPager.setAdapter(new ViewPagerAdapter(listviews));

            /**
             * 动态的对小圆点进行控制
             */
            RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.myinfo_manage_notes_details_head_Relative_spot);

            for (int i = 0; i < results.get(0).getFilesList().size(); i++) {
                LinearLayout linearLayout = new LinearLayout(MyinfoManageOneNoteActivity.this);
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                //设置每个小圆点距离左边的间距

                margin.setMargins(60 * (i + 1), 0, 0, 0);
                imageView = new ImageView(MyinfoManageOneNoteActivity.this);

                //设置每个小圆点的宽高
                imageView.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
                listimageView.add(imageView);
                if (i == 0) {
                    // 默认选中第一张图片
                    listimageView.get(i)
                            .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);
                } else {
                    //其他图片都设置未选中状态
                    listimageView.get(i)
                            .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
                }
                linearLayout.addView(listimageView.get(i), margin);
                mainLayout.addView(linearLayout);
            }
        }
    };

    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(MyinfoManageOneNoteActivity.this, "网络出错——游记详情页", Toast.LENGTH_LONG)
                    .show();
        }
    };


    private void sendComment() {
        sendCommentImg = (ImageView) findViewById(R.id.activity_travel_notes_send_comment_img);
        sendCommentImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                final String id = intent.getStringExtra("id");
                editText = (TextView) findViewById(R.id.myinfo_manage_notes_details_edit_text);
                String comment = editText.getText().toString();
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("uid", String.valueOf(SharePreferenceUtil.getInstance(MyinfoManageOneNoteActivity.this).getKeyData("userId")));
                map.put("content", comment);
                map.put("content", comment);
                if (!comment.isEmpty() && !comment.equals("") && comment != null) {
                    GetVolleyHttp.getInstance(MyinfoManageOneNoteActivity.this).getStringPOSTJsonData(UrlConfig.URL_TRAVEL_NOTES_LIST_COMMENT + id, map,
                            listener1, errorlistener1);
                } else {
                    Toast.makeText(MyinfoManageOneNoteActivity.this, "评论不能为空", Toast.LENGTH_LONG)
                            .show();
                }
                editText.setText("");
            }
        });
    }

    Response.Listener<String> listener1 = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("json", json);
            TravelNotesListPostBean travelNotesListPostBean = JSON.parseObject(json, TravelNotesListPostBean.class);
            if (travelNotesListPostBean.getCode() == 0) {
                commentLinearLayout = (LinearLayout) findViewById(R.id.activity_travel_notes_layout);
                for (int i = 0; i < travelNotesListPostBean.getResults().size(); i++) {
                    commentLinearLayout.addView(createLin(MyinfoManageOneNoteActivity.this, travelNotesListPostBean.getResults().
                            get(i).getName(), travelNotesListPostBean.getResults().get(i).getContent()));
                }

            } else {
                Toast.makeText(MyinfoManageOneNoteActivity.this, "成功", Toast.LENGTH_LONG)
                        .show();
            }
        }
    };
    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener1 = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(MyinfoManageOneNoteActivity.this, "失败", Toast.LENGTH_LONG)
                    .show();
        }
    };

    /**
     * 生成含有两个TextView的LinearLayout
     *
     * @param context
     * @param person
     * @param content
     * @return
     */
    private static LinearLayout createLin(Context context, final String person, final String content) {
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        TextView textViewPerson = new TextView(context);
        TextView textViewContent = new TextView(context);
        textViewPerson.setText(person + "：");
        textViewPerson.setTextColor(Color.BLUE);
        textViewContent.setTextColor(Color.BLACK);
        textViewContent.setText(content);
        linearLayout.addView(textViewPerson, margin);
        linearLayout.addView(textViewContent, margin);
        return linearLayout;
    }

}
